import 'base_presenter.dart';
import 'package:flutterclue/http_util/constant_url.dart';
import 'package:flutterclue/utils/SharePreferensUtil.dart';

class LoginOutPresenter extends ClueBasePresenter<LoginViewOut> {
  ///退出
  void loginOut() async {
    Map<String, Object> map = new Map();
    String agentId =
    await SharePreferencesUtil.getString(SharePreferencesUtil.AgentId);
    String topAgentId =
    await SharePreferencesUtil.getString(SharePreferencesUtil.TopAgentId);
    map["AgentId"] = agentId;
    map["TopAgentId"] = topAgentId;
    baseView.showLoadingView();
    postData(
        url: ApiUrl.Login_URL,
        params: map,
        error: (e) {
          baseView.dismissLoadingView();
          baseView.onLoginFail(e.toString());
        })
        .then((value) {
      baseView.dismissLoadingView();
      if (value.Code == 1) {
        ///清除用户信息
        SharePreferencesUtil.clearUserInfo();
        baseView.onLoginOutSuccess();
      } else {
        baseView.onLoginFail(value.Message);
      }
    });
  }


  ///保存极光账号到服务器
  void saveJpush() async {
    baseView.showLoadingView();
    Map<String, Object> map = new Map();
    map["AgentId"] =
    await SharePreferencesUtil.getString(SharePreferencesUtil.AgentId);
    map["TopAgentId"] =
    await SharePreferencesUtil.getString(SharePreferencesUtil.TopAgentId);
    map["Account"] = "";
    postData(
        url: ApiUrl.Save_Jpush_Account,
        params: map,
        error: (error) {
        });
  }
}

abstract class LoginViewOut extends BaseView {
  void onLoginOutSuccess();

  void onLoginFail(String error);
}
